package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityDaliyReport;
import com.loongtech.bi.manager.system.DailyReportManager;
import com.loongtech.bi.synch.MonitorMailManager;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.Date;
import java.util.List;

@Controller
@Scope("view")
public class DailyReportConfigAction {
    @Resource
    private DailyReportManager dailyReportManager;
    @Resource
    private MonitorMailManager monitorMailManager;

    private EntityDaliyReport entityDaliyReport;
    private List<EntityDaliyReport> entityDaliyReportList;
    private Date testDate = new Date(System.currentTimeMillis() - Time.DAY);

    @Autowired
    private void inital() {
        entityDaliyReportList = dailyReportManager.queryList();
        entityDaliyReport = new EntityDaliyReport();
    }

    public void searchByCriteria() {
        entityDaliyReportList = dailyReportManager.searchByCriteria(entityDaliyReport);
    }

    /**
     * 增加
     */
    public void addEntityDaliyReport() {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        if (validate(faceContext, entityDaliyReport)) {
            dailyReportManager.addEntityDaliyReport(entityDaliyReport);
            faceContext.addMessage(null, new FacesMessage("提示", "添加成功！"));
            inital();
        }
    }


    /**
     * 删除
     */
    public void delEntityDaliyReport(String id) {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        dailyReportManager.delEntityDaliyReport(Integer.parseInt(id));
        faceContext.addMessage(null, new FacesMessage("提示", "删除成功！"));
        inital();
    }

    /**
     * 修改
     */
    public void modifyEntityDaliyReport(EntityDaliyReport e) {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        if (validate(faceContext, e)) {
            dailyReportManager.modifyEntityDaliyReport(e);
            faceContext.addMessage(null, new FacesMessage("提示", "修改成功！"));
            inital();
        }
    }

    /**
     * test mail
     */
    public void test() {
        System.out.println("test mail");
        System.out.println(Time.getDateStr(testDate));
        if (testDate == null) {
            testDate = new Date(System.currentTimeMillis() - Time.DAY);
        }
        monitorMailManager.mail("zhenghaiyuan@zulong.com", "linkmeliu@163.com",Time.getDateStr(testDate));
    }

    private boolean validate(FacesContext faceContext, EntityDaliyReport element) {
        if (Utils.isEmptyStr(element.getInquerySQLOne())) {
            faceContext.addMessage(null, new FacesMessage("提示", "查询语句1不能为空！"));
            return false;
        }

        if (Utils.isEmptyStr(element.getInquerySQLTwo())) {
            faceContext.addMessage(null, new FacesMessage("提示", "查询语句2不能为空！"));
            return false;
        }
        if (null == element.getThreshHold()) {
            faceContext.addMessage(null, new FacesMessage("提示", "阈值不能为空！"));
            return false;
        }
        return true;
    }

    public EntityDaliyReport getEntityDaliyReport() {
        return entityDaliyReport;
    }

    public void setEntityDaliyReport(EntityDaliyReport entityDaliyReport) {
        this.entityDaliyReport = entityDaliyReport;
    }

    public List<EntityDaliyReport> getEntityDaliyReportList() {
        return entityDaliyReportList;
    }

    public void setEntityDaliyReportList(List<EntityDaliyReport> entityDaliyReportList) {
        this.entityDaliyReportList = entityDaliyReportList;
    }

    public Date getTestDate() {
        return testDate;
    }

    public void setTestDate(Date testDate) {
        this.testDate = testDate;
    }
}
